package InterviewOfCollections;

import java.util.*;

/*
 * Author: JacksonCoder
 * FileName: BasicOfSet
 * Version: 1.0.0
 */
public class BasicOfSet {
    public static void main(String[] args) {
//        HashSetExample();
//        LinkedSetExample();
        TreeSetExample();
    }

    public static void TreeSetExample(){
        TreeSet<Integer> numbers = new TreeSet<>();
        numbers.add(5);
        numbers.add(1);
        numbers.add(3);

        System.out.println("Sorted Numbers: " + numbers);
        // 输出: Sorted Numbers: [1, 3, 5]

        // 获取最小和最大值
        System.out.println("First: " + numbers.first());
        System.out.println("Last: " + numbers.last());

        // 自定义排序
        TreeSet<String> words = new TreeSet<>((a, b) -> b.compareTo(a)); // 降序
        words.add("Apple");
        words.add("Banana");
        words.add("Cherry");
        System.out.println("Custom Sorted Words: " + words);
        // 输出: Custom Sorted Words: [Cherry, Banana, Apple]

        TreeSet<String> strings = new TreeSet<>((a,b)->a.compareTo(b));//升序
        strings.add("Alen");
        strings.add("Jackson");
        strings.add("WuSir");
        System.out.println("Custom Sorted strings: " + strings);
        // 输出: Custom Sorted strings: [Alen, Jackson, WuSir]
    }

    public static void LinkedSetExample() {
        LinkedHashSet<String> names = new LinkedHashSet<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        names.add("Alice"); // 重复元素不会被添加

        System.out.println("Names in insertion order: " + names);
        // 输出: Names in insertion order: [Alice, Bob, Charlie]

        // 遍历 LinkedHashSet
        for (String name : names) {
            System.out.println(name);
        }
    }

    public static void HashSetExample() {
        // Set 示例
        Set<String> hashSet = new HashSet<>();
        hashSet.add("A");
        hashSet.add("B");
        hashSet.add("C");
        hashSet.add("D");
        hashSet.add("E");
        hashSet.add("A"); // 不会插入重复元素
        System.out.println("HashSet: " + hashSet); // 输出无序

        hashSet.remove("A");
        System.out.println("HashSet: " + hashSet);

        boolean empty = hashSet.isEmpty();
        System.out.println("HashSet is empty: " + empty);

        System.out.println(hashSet.contains("A"));

        System.out.println(hashSet.size());

    }
}
